﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Bussiness.BannerSlideShow;
using FOModels.BannerSlideShow;
using System.IO;
using Bussiness.Language;
using FOModels.Language;
using FOSystem.CultureHelper;
namespace FO.Controllers
{
    public class BannerSlideShowController : CultureController
    {
        //
        // GET: /BannerSlideShow/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult getBannerSlide()
        {
            BannerSlideShowBLL bll = new BannerSlideShowBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<BannerSlideShowModels> list = new List<BannerSlideShowModels>();
      
            if (lang.LangID == 1)
            {
                list = bll.getBannerSlideShow();
            }
            else
            {
                list = bll.getBannerSlideShow_EN();
            }
           
            return PartialView(list);
        }
        public ActionResult ShowCase()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerSlide()
        {
            return View();
        }
        public JsonResult getSlideJson()
        {
            BannerSlideShowBLL bll = new BannerSlideShowBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<BannerSlideShowModels> list = new List<BannerSlideShowModels>();
            if (lang.LangID == 1)
            {
                list = bll.getBannerSlideShowAll();
            }
            else
            {
                list = bll.getBannerSlideShowAll_EN();
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertSlide()
        {
            BannerSlideShowBLL cardbll = new BannerSlideShowBLL();
            List<TargetModels> com = new List<TargetModels>();
            com.Add(new TargetModels { ID = 1, Name = "_blank" });
            com.Add(new TargetModels { ID = 1, Name = "_parent" });
            com.Add(new TargetModels { ID = 1, Name = "_self" });
            com.Add(new TargetModels { ID = 1, Name = "_top" });
            ViewBag.Target = new SelectList(com, "Name", "Name");
            return View();

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertSlide(FO.Models.BannerSlideShow card)
        {
            BannerSlideShowBLL cardbll = new BannerSlideShowBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<TargetModels> com = new List<TargetModels>();
            com.Add(new TargetModels { ID = 1, Name = "_blank" });
            com.Add(new TargetModels { ID = 1, Name = "_parent" });
            com.Add(new TargetModels { ID = 1, Name = "_self" });
            com.Add(new TargetModels { ID = 1, Name = "_top" });
            ViewBag.Target = new SelectList(com, "Name", "Name");
            if (ModelState.IsValid)
            {
                BannerSlideShowModels items = new BannerSlideShowModels();
                items.Title = card.Title;
                items.Images = card.Images;
                items.Target = card.Target;
                items.Links = card.Links;
                items.Enable = card.Enable;
                if (lang.LangID == 1)
                {
                    cardbll.InsertSlide(items);
                }
                else
                {
                    cardbll.InsertSlide_EN(items);
                }
                return RedirectToAction("ManagerSlide");
            }
            return View(card);

        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateSlide(int SShowID)
        {
            BannerSlideShowBLL cardbll = new BannerSlideShowBLL();
            List<TargetModels> com = new List<TargetModels>();
            com.Add(new TargetModels { ID = 1, Name = "_blank" });
            com.Add(new TargetModels { ID = 1, Name = "_parent" });
            com.Add(new TargetModels { ID = 1, Name = "_self" });
            com.Add(new TargetModels { ID = 1, Name = "_top" });
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BannerSlideShowModels card = new BannerSlideShowModels();
            if (lang.LangID == 1)
            {
                card = cardbll.getslideshowbyid(SShowID);
            }
            else
            {
                card = cardbll.getslideshowbyid_EN(SShowID);
            }
            ViewBag.Target = new SelectList(com, "Name", "Name", card.Target);
            Models.BannerSlideShow c = new Models.BannerSlideShow();
            c.Title = card.Title;
            c.Images = card.Images;
            c.Target = card.Target;
            c.Links = card.Links;
            c.Enable = card.Enable;
            c.OrderSort = card.OrderSort;
            return View(c);
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateSlide(FO.Models.BannerSlideShow card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BannerSlideShowBLL cardbll = new BannerSlideShowBLL();
            List<TargetModels> com = new List<TargetModels>();
            com.Add(new TargetModels { ID = 1, Name = "_blank" });
            com.Add(new TargetModels { ID = 1, Name = "_parent" });
            com.Add(new TargetModels { ID = 1, Name = "_self" });
            com.Add(new TargetModels { ID = 1, Name = "_top" });
            BannerSlideShowModels ca = new BannerSlideShowModels();
            if (lang.LangID == 1)
            {
                ca = cardbll.getslideshowbyid(card.SShowID);
                ViewBag.Target = new SelectList(com, "Name", "Name");
                if (ca != null)
                {
                    ca.Title = card.Title;
                    ca.Images = card.Images;
                    ca.Target = card.Target;
                    ca.Links = card.Links;
                    ca.Enable = card.Enable;
                    ca.OrderSort = card.OrderSort;
                    cardbll.UpdateSlide(ca);
                    return RedirectToAction("ManagerSlide");
                }
            }
            else
            {
                ca = cardbll.getslideshowbyid_EN(card.SShowID);
                ViewBag.Target = new SelectList(com, "Name", "Name");
                if (ca != null)
                {
                    ca.Title = card.Title;
                    ca.Images = card.Images;
                    ca.Target = card.Target;
                    ca.Links = card.Links;
                    ca.Enable = card.Enable;
                    ca.OrderSort = card.OrderSort;
                    cardbll.UpdateSlide_EN(ca);
                    return RedirectToAction("ManagerSlide");
                }
            }
            return View();


        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteSlide(int SShowID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BannerSlideShowBLL bll = new BannerSlideShowBLL();
            BannerSlideShowModels card = new BannerSlideShowModels();
            if (lang.LangID == 1)
            {
                card = bll.getslideshowbyid(SShowID);
                if (card != null)
                {
                    bll.DeleteSlide(card.SShowID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                card = bll.getslideshowbyid_EN(SShowID);
                if (card != null)
                {
                    bll.DeleteSlide_EN(card.SShowID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }

        }
        
    }
}
